package com.itheima.appserver.controller;

import com.itheima.appserver.config.NoAuthorization;
import com.itheima.appserver.service.UserInfoService;
import com.itheima.appserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author yaohu
 * @date 2021/12/3
 * @Version 1.0
 * @describe
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 登陆
     *
     * @param params
     * @return
     */
    @NoAuthorization
    @PostMapping("/login")
    public ResponseEntity userLogin(@RequestBody Map<String, String> params) {
        // 通常来说我们要把所有接口都统一返回值
        // UNAUTHORIZED : 401 未授权 未登录
        // INTERNAL_SERVER_ERROR : 500 服务器内部错误
        userService.sendLoginMsg(params.get("phone"));
        return ResponseEntity.ok(null);
    }

    /**
     * 校验验证码
     *
     * @param params
     * @return
     */
    @NoAuthorization
    @PostMapping("/loginVerification")
    public ResponseEntity userLoginVerification(@RequestBody Map<String, String> params) {
        Map<String, Object> map = userService.loginVerification(params.get("phone"), params.get("verificationCode"));
        return ResponseEntity.ok(map);
    }

    /**
     * 完善用户信息
     *
     * @param params
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity userLoginReginfo(@RequestBody Map<String, String> params) {
        userInfoService.loginReginfo(params);
        return ResponseEntity.ok(null);
    }

    /**
     * 上传头像
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity userLoginReginfoHead(@RequestParam("headPhoto") MultipartFile headPhoto) {
        userInfoService.loginReginfoHead(headPhoto);
        return ResponseEntity.ok(null);
    }
}
